package ciyuan.Block.Machine.gl;

import ciyuan.Gui.ContainerRegistry;
import ciyuan.Item.ItemRegisterManager;
import ciyuan.Utils.TileEntityRegisterManager;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.INamedContainerProvider;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.ITickableTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IIntArray;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;

import javax.annotation.Nullable;

/**
 * @author 150149
 */
public class GlTileEntity extends TileEntity implements ITickableTileEntity, INamedContainerProvider {

    private final Inventory inventory = new Inventory(3);
    public ContainerProgress containerProgress = new ContainerProgress();

    public GlTileEntity() {
        super(TileEntityRegisterManager.gl.get());
    }

    @Override
    public ITextComponent getDisplayName() {
        return new TranslationTextComponent("ciyuan.container.dsc3");
    }

    @Nullable
    @Override
    public Container createMenu(int sycID, PlayerInventory inventory, PlayerEntity player) {
        return new GlContainer(ContainerRegistry.glContainer.get(), sycID, inventory, getBlockPos());
    }

    @Override
    public void load(BlockState state, CompoundNBT nbt) {
        this.inventory.setItem(0,ItemStack.of(nbt.getCompound("item")));
        this.inventory.setItem(1,ItemStack.of(nbt.getCompound("item2")));
        this.inventory.setItem(2,ItemStack.of(nbt.getCompound("item3")));
        super.load(state, nbt);
    }

    @Override
    public CompoundNBT save(CompoundNBT compound) {
        ItemStack itemStack = this.inventory.getItem(0).copy();
        ItemStack itemStack2 = this.inventory.getItem(1).copy();
        ItemStack itemStack3 = this.inventory.getItem(2).copy();
        compound.put("item", itemStack.serializeNBT());
        compound.put("item2", itemStack2.serializeNBT());
        compound.put("item3", itemStack3.serializeNBT());
        return super.save(compound);
    }

    public Inventory getInventory() {
        return inventory;
    }

    @Override
    public void tick() {
        if (!this.inventory.getItem(0).isEmpty()) {
            if (this.inventory.getItem(0).getItem().getDescriptionId().equals(Items.IRON_INGOT.getDescriptionId()) && (this.inventory.getItem(2).isEmpty()||(this.inventory.getItem(2).getItem().getDescriptionId().equals(ItemRegisterManager.Steel.get().getDescriptionId()) && this.inventory.getItem(2).getCount()<64)) ) {
                if (this.containerProgress.get(2)>0) {
                    if (this.containerProgress.get(0)>=800) {
                        this.inventory.getItem(0).setCount(this.inventory.getItem(0).getCount()-1);
                        this.inventory.addItem(new ItemStack(ItemRegisterManager.Steel.get()));
                        this.containerProgress.set(0,0);
                        this.containerProgress.set(1,0);
                    } else {
                        this.containerProgress.set(0,this.containerProgress.get(0)+1);
                        this.containerProgress.set(2,this.containerProgress.get(2)-1);
                        this.containerProgress.set(1,1);
                    }
                } else {
                    if (!this.inventory.getItem(1).isEmpty()) {
                        if (this.inventory.getItem(1).getItem().getDescriptionId().equals(Items.COAL.getDescriptionId())) {
                            this.inventory.getItem(1).setCount(this.inventory.getItem(1).getCount()-1);
                            this.containerProgress.set(2,this.containerProgress.get(2)+1000);
                        } else if (this.inventory.getItem(1).getItem().getDescriptionId().equals(ItemRegisterManager.anthracite.get().getDescriptionId())) {
                            this.inventory.getItem(1).setCount(this.inventory.getItem(1).getCount()-1);
                            this.containerProgress.set(2,this.containerProgress.get(2)+1600);
                        } else if (this.inventory.getItem(1).getItem().getDescriptionId().equals(ItemRegisterManager.lignite.get().getDescriptionId())) {
                            this.inventory.getItem(1).setCount(this.inventory.getItem(1).getCount()-1);
                            this.containerProgress.set(2,this.containerProgress.get(2)+500);
                        }
                    }
                }
            }
        } else {
            this.containerProgress.set(0,0);
            this.containerProgress.set(1,0);
        }
    }

    public class ContainerProgress implements IIntArray {

        int[] progress = {0,0,0};

        @Override
        public int get(int index) {
            return progress[index];
        }

        @Override
        public void set(int index, int value) {
            progress[index] = value;
        }

        @Override
        public int getCount() {
            return 3;
        }
    }
}
